package com.ezjie.ielts.db;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ezjie.ielts.model.EwordInstance;
import com.ezjie.ielts.util.DownloadUtil;
import com.ezjie.ielts.util.LogUtils;

/**
 * 单词的 dao操作
 * 
 * @author renyangyang
 *
 */
public class WordInstanceDao {
	private static final String TABLE_NAME = "e_word_instance";
	private static final String COLUMN_INSTANCE_ID = "instance_id";
	private static final String COLUMN_WORD_ID = "word_id";
	private static final String COLUMN_SOURCE_ID = "source_id";
	private static final String COLUMN_LANG = "lang";
	private static final String COLUMN_ORIGIN = "origin";
	private static final String COLUMN_TRANSLATION = "translation";
	private static final String COLUMN_IS_ACTIVE = "is_active";
	private static final String COLUMN_CREATE_TIME = "create_time";
	private static final String COLUMN_UPDATE_TIME = "update_time";
	private SQLiteDatabase database;

	public WordInstanceDao(Context context) {
		DownloadUtil.initCacheDir(context);
		String databaseFilename = DownloadUtil.WORD_CACHE_PATH + "/" + DownloadUtil.WORD_DATABASE_FILENAME;
		File file = new File(databaseFilename);
		if (!file.exists()) {
			// 如果数据库文件不存在 就再拷贝一份
			DownloadUtil.getInstance().copyWordDB2SD(context);
		}

		if (file.exists()) {
			database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
		}
	}

	/**
	 * 根据单词id 查找单词
	 * 
	 * @param word_id
	 * @return
	 */
	public List<EwordInstance> findByWordId(int word_id) {
		List<EwordInstance> list = new ArrayList<EwordInstance>();
		if (database == null) {
			return list;
		}
		Cursor cursor = database.query(TABLE_NAME, null, COLUMN_WORD_ID + "=? and " + COLUMN_IS_ACTIVE + "=?",
				new String[] { word_id + "", 1 + "" }, null, null, null);
		while (cursor.moveToNext()) {
			EwordInstance ewordInstance = new EwordInstance();
			ewordInstance.setInstance_id(cursor.getInt(cursor.getColumnIndex(COLUMN_INSTANCE_ID)));
			ewordInstance.setWord_id(cursor.getInt(cursor.getColumnIndex(COLUMN_WORD_ID)));
			ewordInstance.setSource_id(cursor.getInt(cursor.getColumnIndex(COLUMN_SOURCE_ID)));
			ewordInstance.setLang(cursor.getString(cursor.getColumnIndex(COLUMN_LANG)));
			ewordInstance.setOrigin(cursor.getString(cursor.getColumnIndex(COLUMN_ORIGIN)));
			ewordInstance.setTranslation(cursor.getString(cursor.getColumnIndex(COLUMN_TRANSLATION)));
			ewordInstance.setIs_active(cursor.getInt(cursor.getColumnIndex(COLUMN_IS_ACTIVE)));
			ewordInstance.setCreate_time(cursor.getString(cursor.getColumnIndex(COLUMN_CREATE_TIME)));
			ewordInstance.setUpdate_time(cursor.getString(cursor.getColumnIndex(COLUMN_UPDATE_TIME)));
			list.add(ewordInstance);
		}
		cursor.close();
		return list;
	}

	public EwordInstance findByInstanceId(int instance_id) {
		EwordInstance ewordInstance = new EwordInstance();
		if (database == null) {
			return ewordInstance;
		}
		Cursor cursor = database.query(TABLE_NAME, null, COLUMN_INSTANCE_ID + "=?", new String[] { instance_id + "" },
				null, null, null);
		while (cursor.moveToNext()) {
			ewordInstance.setInstance_id(cursor.getInt(cursor.getColumnIndex(COLUMN_INSTANCE_ID)));
			ewordInstance.setWord_id(cursor.getInt(cursor.getColumnIndex(COLUMN_WORD_ID)));
			ewordInstance.setSource_id(cursor.getInt(cursor.getColumnIndex(COLUMN_SOURCE_ID)));
			ewordInstance.setLang(cursor.getString(cursor.getColumnIndex(COLUMN_LANG)));
			ewordInstance.setOrigin(cursor.getString(cursor.getColumnIndex(COLUMN_ORIGIN)));
			ewordInstance.setTranslation(cursor.getString(cursor.getColumnIndex(COLUMN_TRANSLATION)));
			ewordInstance.setIs_active(cursor.getInt(cursor.getColumnIndex(COLUMN_IS_ACTIVE)));
			ewordInstance.setCreate_time(cursor.getString(cursor.getColumnIndex(COLUMN_CREATE_TIME)));
			ewordInstance.setUpdate_time(cursor.getString(cursor.getColumnIndex(COLUMN_UPDATE_TIME)));

		}
		cursor.close();
		return ewordInstance;
	}

	public EwordInstance findByInstanceIdOrNull(int instance_id) {
		EwordInstance ewordInstance = null;
		if (database == null) {
			return ewordInstance;
		}
		Cursor cursor = database.query(TABLE_NAME, null, COLUMN_INSTANCE_ID + "=?", new String[] { instance_id + "" },
				null, null, null);
		while (cursor.moveToNext()) {
			ewordInstance = new EwordInstance();
			ewordInstance.setInstance_id(cursor.getInt(cursor.getColumnIndex(COLUMN_INSTANCE_ID)));
			ewordInstance.setWord_id(cursor.getInt(cursor.getColumnIndex(COLUMN_WORD_ID)));
			ewordInstance.setSource_id(cursor.getInt(cursor.getColumnIndex(COLUMN_SOURCE_ID)));
			ewordInstance.setLang(cursor.getString(cursor.getColumnIndex(COLUMN_LANG)));
			ewordInstance.setOrigin(cursor.getString(cursor.getColumnIndex(COLUMN_ORIGIN)));
			ewordInstance.setTranslation(cursor.getString(cursor.getColumnIndex(COLUMN_TRANSLATION)));
			ewordInstance.setIs_active(cursor.getInt(cursor.getColumnIndex(COLUMN_IS_ACTIVE)));
			ewordInstance.setCreate_time(cursor.getString(cursor.getColumnIndex(COLUMN_CREATE_TIME)));
			ewordInstance.setUpdate_time(cursor.getString(cursor.getColumnIndex(COLUMN_UPDATE_TIME)));

		}
		cursor.close();
		return ewordInstance;
	}

	public void insert(EwordInstance eword) {
		if (database != null && eword != null) {
			ContentValues values = new ContentValues();
			values.put(COLUMN_INSTANCE_ID, eword.getInstance_id());
			values.put(COLUMN_WORD_ID, eword.getWord_id());
			values.put(COLUMN_SOURCE_ID, eword.getSource_id());
			values.put(COLUMN_LANG, eword.getLang());
			values.put(COLUMN_ORIGIN, eword.getOrigin());
			values.put(COLUMN_TRANSLATION, eword.getTranslation());
			values.put(COLUMN_IS_ACTIVE, eword.getIs_active());
			values.put(COLUMN_CREATE_TIME, eword.getCreate_time());
			values.put(COLUMN_UPDATE_TIME, eword.getUpdate_time());
			long insert = database.insert(TABLE_NAME, null, values);
			if (insert != -1) {
				LogUtils.d("插入wordInstance成功");
			} else {
				LogUtils.d("插入wordInstance失败");
			}
		}
	}

	public void update(EwordInstance eword) {
		if (database != null && eword != null) {
			ContentValues values = new ContentValues();
			values.put(COLUMN_INSTANCE_ID, eword.getInstance_id());
			values.put(COLUMN_WORD_ID, eword.getWord_id());
			values.put(COLUMN_SOURCE_ID, eword.getSource_id());
			values.put(COLUMN_LANG, eword.getLang());
			values.put(COLUMN_ORIGIN, eword.getOrigin());
			values.put(COLUMN_TRANSLATION, eword.getTranslation());
			values.put(COLUMN_IS_ACTIVE, eword.getIs_active());
			values.put(COLUMN_CREATE_TIME, eword.getCreate_time());
			values.put(COLUMN_UPDATE_TIME, eword.getUpdate_time());
			int update = database.update(TABLE_NAME, values, COLUMN_INSTANCE_ID + "=?",
					new String[] { eword.getInstance_id() + "" });
		}
	}

	public void updateList(List<EwordInstance> list) {
		for (EwordInstance ewordInstance : list) {
			EwordInstance findByInstanceId = findByInstanceIdOrNull(ewordInstance.getInstance_id());
			if (findByInstanceId == null) {
				insert(ewordInstance);
			} else {
				update(ewordInstance);
			}
		}
	}

}
